{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1 Numpy基础"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n(3,)\n1 2 3\n[5 2 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1, 2, 3])  # 创建一维数组\n",
    "print(type(a))            # 打印类型\n",
    "print(a.shape)            # 打印数组形状\n",
    "print(a[0], a[1], a[2])   # 打印数组元素\n",
    "a[0] = 5                 # 改变元素取值\n",
    "print(a)                  # 打印整个数组\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n [4 5 6]]\n(2, 3)\n1 2 4\n"
     ]
    }
   ],
   "source": [
    "\n",
    "b = np.array([[1,2,3],[4,5,6]]) \n",
    "print(b)# 创建二维数组\n",
    "print(b.shape)                     # 形状\n",
    "print(b[0, 0], b[0, 1], b[1, 0])   # 打印数组元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "float64 [1. 2. 3.]\tint32 [1 2 3]\n"
     ]
    }
   ],
   "source": [
    "#numpy的数据类型设置\n",
    "arr1 = np.array([1,2,3],dtype=np.float64)\n",
    "arr2 = np.array([1,2,3],dtype=np.int32)\n",
    "print(arr1.dtype,arr1,end=\"\\t\")\n",
    "print(arr2.dtype,arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int64\n",
      "float64\n"
     ]
    }
   ],
   "source": [
    "#类型转换\n",
    "arr=np.array([1,2,3])\n",
    "print(arr.dtype)\n",
    "\n",
    "float_arr=arr.astype(np.float64)\n",
    "print(float_arr.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n[5 7]\n[  5 999]\n[14 14]\n"
     ]
    }
   ],
   "source": [
    "#索引和切片\n",
    "arr = np.arange(10)\n",
    "print(arr)\n",
    "arr_slice = arr[5:8:2]\n",
    "print(arr_slice)\n",
    "arr_slice[1] = 999\n",
    "print(arr_slice)\n",
    "\n",
    "arr_slice[:] = 14\n",
    "print(arr_slice)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Bob' 'Will' 'Bob' 'Joe']\n[[-3.   2.   0.  -1. ]\n [ 1.   2.   3.  -4. ]\n [ 2.5  1.7 -0.2  1. ]\n [-8.  -4.   9.  10. ]]\n[ True False  True False]\n[[-3.   2.   0.  -1. ]\n [ 2.5  1.7 -0.2  1. ]]\n[[ 0.  -1. ]\n [-0.2  1. ]]\n[[-3.   2.   0.  -1. ]\n [ 1.   2.   3.  -4. ]\n [ 2.5  1.7 -0.2  1. ]\n [-8.  -4.   9.  10. ]]\n[[-3.   1.   2.5 -8. ]\n [ 2.   2.   1.7 -4. ]\n [ 0.   3.  -0.2  9. ]\n [-1.  -4.   1.  10. ]]\n"
     ]
    }
   ],
   "source": [
    "# 利用布尔数组切片\n",
    "names = np.array(['Bob', 'Will', 'Bob', 'Joe'])\n",
    "data = np.array([[-3, 2, 0, -1], [1, 2, 3, -4],\n",
    "                 [2.5, 1.7, -0.2, 1], [-8, -4, 9, 10]])\n",
    "print(names)\n",
    "print(data)\n",
    "print(names == 'Bob')\n",
    "print(data[names == 'Bob'])\n",
    "print(data[names == 'Bob', 2:])\n",
    "\n",
    "print(data)\n",
    "print(data.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n",
      "9.5\n",
      "190\n",
      "[  2.   7.  12.  17.]\n",
      "[30 34 38 42 46]\n"
     ]
    }
   ],
   "source": [
    "# 基本统计方法\n",
    "arr = np.arange(20).reshape((4,5))\n",
    "print(arr)\n",
    "print(arr.mean()) # 求均值\n",
    "print(arr.sum()) # 求和\n",
    "print(arr.mean(axis = 1)) # 沿列求均值\n",
    "print(arr.sum(axis = 0)) # 沿行求和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 正态分布采样\n",
    "samples = np.random.normal(size=(2,3))\n",
    "print(samples)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本案例使用lendingclub的公开数据集，完整数据集参考[Kaggle竞赛:Lending Club Loan Data](https://www.kaggle.com/wendykan/lending-club-loan-data)简要介绍pandas用于数据分析的方法和步骤。\n",
    "\n",
    "数据集为贷款用户数据，有以下属性\n",
    "\n",
    "|loan_amnt|grade|sub_grade|emp_length|home_onwership|annual_inc|issue_d|loan_status|open_acc|total_pymnt|total_rec_int|\n",
    "|:---:|:---:|:---:|:---:|:---:|:---:|\n",
    "|贷款数额|评级|评级细分|工作年限|是否拥有房屋|年收入|发贷日期|贷款状态|开户数量|还款总额|还贷利息总额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   loan_amnt grade sub_grade emp_length home_ownership  annual_inc  \\\n0     5000.0     B        B2  10+ years           RENT     24000.0   \n1     2500.0     C        C4   < 1 year           RENT     30000.0   \n2    12500.0     D        D4  10+ years           RENT     74400.0   \n3    14000.0     A        A4  10+ years           RENT     72000.0   \n4    35000.0     F        F5    4 years       MORTGAGE    100000.0   \n5    16000.0     C        C2  10+ years            OWN         NaN   \n6    14125.0     B        B5  10+ years            OWN     60000.0   \n7     5000.0     B        B2    4 years           RENT     49000.0   \n8     6000.0     A        A5  10+ years       MORTGAGE     48000.0   \n9    12000.0     C        C3   < 1 year       MORTGAGE     81996.0   \n\n      issue_d  loan_status  open_acc   total_pymnt  total_rec_int  \n0  2017/12/11   Fully Paid         3   5863.155187         863.16  \n1  2017/12/11  Charged Off         3   1014.530000         435.17  \n2  2017/11/11   Fully Paid         8  14722.411910        2222.41  \n3  2017/11/11   fully Paid         6  15099.982530        1099.98  \n4  2017/10/11   Fully Paid         9  48743.119270       13743.12  \n5  2017/10/11  Charged Off        13   9202.000000        3800.05  \n6  2017/10/11   Fully Paid        13  16750.738520        2625.74  \n7   2017/9/11   Fully Paid        10   5863.155187         863.16  \n8   2017/9/11   Fully Paid        11   7235.598134        1235.60  \n9  2017/10/11   Fully Paid        11  16996.739990        4996.74  \n"
     ]
    }
   ],
   "source": [
    "# 读入数据\n",
    "loan_data = pd.read_csv('./data/loandata.csv')\n",
    "print(loan_data.head(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2 Series和DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.series.Series'>\n<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "# Series对象\n",
    "amnt_series = loan_data['loan_amnt']\n",
    "print(type(amnt_series))\n",
    "# dataframe对象\n",
    "print(type(loan_data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   f1_score         method\n",
      "0      0.73  decision tree\n",
      "1      0.89            svm\n"
     ]
    }
   ],
   "source": [
    "# 创建dataframe对象 - 字典传入\n",
    "data = {'method':['decision tree','svm'],'f1_score':[0.73,0.89]}\n",
    "df = pd.DataFrame(data)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   f1_score      algorithm\n",
      "0      0.73  decision tree\n",
      "1      0.89            svm\n"
     ]
    }
   ],
   "source": [
    "df.rename(columns={'method':'algorithm'},inplace=True)# 更改列名\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3 索引、选取、过滤、排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1_score    0.89\n",
      "method       svm\n",
      "Name: 1, dtype: object\n",
      "0    decision tree\n",
      "1              svm\n",
      "Name: method, dtype: object\n",
      "   f1_score method\n",
      "1      0.89    svm\n",
      "   f1_score\n",
      "0      0.73\n",
      "1      0.89\n"
     ]
    }
   ],
   "source": [
    "print(df.iloc[1])# 获取某一行\n",
    "print(df['method'])# 获取某一列\n",
    "print(df.drop(0,axis=0))# drop 丢弃某一行 或 某一列 \n",
    "print(df.drop('method',axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   f1_score method\n",
      "1      0.89    svm\n"
     ]
    }
   ],
   "source": [
    "df.drop(0,axis=0,inplace=True)# 丢弃之后更新dataframe\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_amnt</th>\n",
       "      <th>grade</th>\n",
       "      <th>sub_grade</th>\n",
       "      <th>emp_length</th>\n",
       "      <th>home_ownership</th>\n",
       "      <th>annual_inc</th>\n",
       "      <th>issue_d</th>\n",
       "      <th>loan_status</th>\n",
       "      <th>open_acc</th>\n",
       "      <th>total_pymnt</th>\n",
       "      <th>total_rec_int</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A4</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>72000.0</td>\n",
       "      <td>2017/11/11</td>\n",
       "      <td>fully Paid</td>\n",
       "      <td>6</td>\n",
       "      <td>15099.982530</td>\n",
       "      <td>1099.98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>6000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>48000.0</td>\n",
       "      <td>2017/9/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>11</td>\n",
       "      <td>7235.598134</td>\n",
       "      <td>1235.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>12800.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A3</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>100000.0</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>10</td>\n",
       "      <td>14211.312910</td>\n",
       "      <td>1411.31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>2100.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>1 year</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>72000.0</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>15</td>\n",
       "      <td>2357.178487</td>\n",
       "      <td>257.18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>18000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>82000.0</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>20204.461240</td>\n",
       "      <td>2204.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>18000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>82000.0</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>20204.461240</td>\n",
       "      <td>2204.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>NaN</td>\n",
       "      <td>A</td>\n",
       "      <td>A4</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>58000.0</td>\n",
       "      <td>2017/1/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>12</td>\n",
       "      <td>10994.614810</td>\n",
       "      <td>994.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>12000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>1 year</td>\n",
       "      <td>RENT</td>\n",
       "      <td>105000.0</td>\n",
       "      <td>2017/1/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>10</td>\n",
       "      <td>13041.191490</td>\n",
       "      <td>1041.19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>5000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A4</td>\n",
       "      <td>9 years</td>\n",
       "      <td>OWN</td>\n",
       "      <td>56400.0</td>\n",
       "      <td>2017/1/11</td>\n",
       "      <td>Charged Off</td>\n",
       "      <td>15</td>\n",
       "      <td>3927.470000</td>\n",
       "      <td>465.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>15000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>55000.0</td>\n",
       "      <td>2017/10/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>14</td>\n",
       "      <td>16274.762910</td>\n",
       "      <td>1274.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>14000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A3</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>OWN</td>\n",
       "      <td>71000.0</td>\n",
       "      <td>2017/10/10</td>\n",
       "      <td>fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>15595.389910</td>\n",
       "      <td>1595.39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>6600.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>55800.0</td>\n",
       "      <td>2017/7/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>7349.816860</td>\n",
       "      <td>749.82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>2000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A1</td>\n",
       "      <td>n/a</td>\n",
       "      <td>OWN</td>\n",
       "      <td>16000.0</td>\n",
       "      <td>2017/7/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>9</td>\n",
       "      <td>2203.348430</td>\n",
       "      <td>203.35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>6000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>82000.0</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>17</td>\n",
       "      <td>6745.258045</td>\n",
       "      <td>745.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>6000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>82000.0</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>17</td>\n",
       "      <td>6745.258045</td>\n",
       "      <td>745.26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>8000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A4</td>\n",
       "      <td>5 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>38000.0</td>\n",
       "      <td>2017/11/9</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>6</td>\n",
       "      <td>8322.639678</td>\n",
       "      <td>322.64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>2000.0</td>\n",
       "      <td>A</td>\n",
       "      <td>A2</td>\n",
       "      <td>2 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>44000.0</td>\n",
       "      <td>2017/11/9</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>11</td>\n",
       "      <td>2207.212632</td>\n",
       "      <td>207.21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    loan_amnt grade sub_grade emp_length home_ownership  annual_inc  \\\n",
       "3     14000.0     A        A4  10+ years           RENT     72000.0   \n",
       "8      6000.0     A        A5  10+ years       MORTGAGE     48000.0   \n",
       "18    12800.0     A        A3  10+ years       MORTGAGE    100000.0   \n",
       "19     2100.0     A        A5     1 year       MORTGAGE     72000.0   \n",
       "25    18000.0     A        A5    8 years       MORTGAGE     82000.0   \n",
       "28    18000.0     A        A5    8 years       MORTGAGE     82000.0   \n",
       "29        NaN     A        A4  10+ years           RENT     58000.0   \n",
       "31    12000.0     A        A5     1 year           RENT    105000.0   \n",
       "32     5000.0     A        A4    9 years            OWN     56400.0   \n",
       "38    15000.0     A        A5  10+ years       MORTGAGE     55000.0   \n",
       "41    14000.0     A        A3  10+ years            OWN     71000.0   \n",
       "43     6600.0     A        A5    8 years       MORTGAGE     55800.0   \n",
       "46     2000.0     A        A1        n/a            OWN     16000.0   \n",
       "49     6000.0     A        A5    8 years           RENT     82000.0   \n",
       "54     6000.0     A        A5    8 years           RENT     82000.0   \n",
       "64     8000.0     A        A4    5 years       MORTGAGE     38000.0   \n",
       "65     2000.0     A        A2    2 years       MORTGAGE     44000.0   \n",
       "\n",
       "       issue_d  loan_status  open_acc   total_pymnt  total_rec_int  \n",
       "3   2017/11/11   fully Paid         6  15099.982530        1099.98  \n",
       "8    2017/9/11   Fully Paid        11   7235.598134        1235.60  \n",
       "18   2017/4/11   Fully Paid        10  14211.312910        1411.31  \n",
       "19   2017/4/11   Fully Paid        15   2357.178487         257.18  \n",
       "25   2017/4/11   Fully Paid         8  20204.461240        2204.46  \n",
       "28   2017/4/11   Fully Paid         8  20204.461240        2204.46  \n",
       "29   2017/1/11   Fully Paid        12  10994.614810         994.61  \n",
       "31   2017/1/11   Fully Paid        10  13041.191490        1041.19  \n",
       "32   2017/1/11  Charged Off        15   3927.470000         465.75  \n",
       "38  2017/10/10   Fully Paid        14  16274.762910        1274.76  \n",
       "41  2017/10/10   fully Paid         8  15595.389910        1595.39  \n",
       "43   2017/7/10   Fully Paid         8   7349.816860         749.82  \n",
       "46   2017/7/10   Fully Paid         9   2203.348430         203.35  \n",
       "49   2017/5/10   Fully Paid        17   6745.258045         745.26  \n",
       "54   2017/5/10   Fully Paid        17   6745.258045         745.26  \n",
       "64   2017/11/9   Fully Paid         6   8322.639678         322.64  \n",
       "65   2017/11/9   Fully Paid        11   2207.212632         207.21  "
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 布尔型数据过滤数据\n",
    "loan_data[loan_data['grade']=='A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  grade sub_grade emp_length home_ownership     issue_d  loan_status\n0     B        B2  10+ years           RENT  2017/12/11   Fully Paid\n1     C        C4   < 1 year           RENT  2017/12/11  Charged Off\n"
     ]
    }
   ],
   "source": [
    "# 只选择其中特定的类型进行处理，如数字编码\n",
    "print(loan_data.select_dtypes(include=['object']).head(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "loan_object = loan_data.select_dtypes(include=['object'])# 选择指定类型的列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   grade sub_grade emp_length home_ownership     issue_d    loan_status\n4      F        F5    4 years       MORTGAGE  2017/10/11     Fully Paid\n20     F        F2    9 years           RENT   2017/4/11    Charged Off\n56     E        E1  10+ years           RENT   2017/5/10     Fully Paid\n51     E        E1  10+ years           RENT   2017/5/10     Fully Paid\n27     E        E1  10+ years           RENT   2017/3/11     Fully Paid\n26     E        E1     1 year       MORTGAGE   2017/4/11     Fully Paid\n24     E        E1  10+ years           RENT   2017/3/11     Fully Paid\n23     E        E1     1 year       MORTGAGE   2017/4/11     Fully Paid\n34     D        D5   < 1 year           RENT   2017/1/11     Fully Paid\n15     D        D2    4 years           RENT   2017/5/11    Charged Off\n58     D        D2    2 years           RENT   2017/4/10     Fully Paid\n47     D        D4  10+ years       MORTGAGE   2017/7/10     fully Paid\n37     D        D3   < 1 year       MORTGAGE   2017/5/11    Charged Off\n17     D        D3   < 1 year       MORTGAGE   2017/5/11    Charged Off\n67     D        D4    8 years           RENT   2017/5/11     Fully Paid\n14     D        D4    8 years           RENT   2017/5/11     Fully Paid\n2      D        D4  10+ years           RENT  2017/11/11     Fully Paid\n12     D        D5    6 years       MORTGAGE   2017/6/11     Fully Paid\n10     D        D4  10+ years       MORTGAGE   2017/6/11     Fully Paid\n48     C        C3    6 years            OWN   2017/5/10     Fully Paid\n16     C        C2  10+ years           RENT   2017/5/11     Fully Paid\n40     C        C5  10+ years           RENT   2017/9/10     Fully Paid\n5      C        C2  10+ years            OWN  2017/10/11    Charged Off\n36     C        C2    2 years       MORTGAGE   2017/9/10     Fully Paid\n35     C        C1  10+ years           RENT   2017/9/10     fully Paid\n1      C        C4   < 1 year           RENT  2017/12/11    Charged Off\n33     C        C5    3 years       MORTGAGE   2017/1/11     Fully Paid\n53     C        C3    6 years            OWN   2017/5/10     Fully Paid\n30     C        C2  10+ years       MORTGAGE   2017/1/11     Fully Paid\n9      C        C3   < 1 year       MORTGAGE  2017/10/11     Fully Paid\n..   ...       ...        ...            ...         ...            ...\n61     B        B5  10+ years       MORTGAGE   2017/12/9     Fully Paid\n62     B        B5    2 years           RENT   2017/12/9     Fully Paid\n52     B        B2    2 years           RENT   2017/5/10     Fully Paid\n50     B        B3  10+ years       MORTGAGE   2017/5/10     Fully Paid\n63     B        B1    5 years       MORTGAGE   2017/11/9     Fully Paid\n66     B        B5    2 years           RENT   2017/12/9     Fully Paid\n6      B        B5  10+ years            OWN  2017/10/11     Fully Paid\n0      B        B2  10+ years           RENT  2017/12/11     Fully Paid\n42     B        B5     1 year           RENT   2017/9/10     Fully Paid\n39     B        B2    5 years       MORTGAGE   2017/9/10     Fully Paid\n7      B        B2    4 years           RENT   2017/9/11     Fully Paid\n22     B        B5    9 years       MORTGAGE   2017/3/11    charged Off\n11     B        B4     1 year           RENT   2017/6/11    Charged Off\n46     A        A1        NaN            OWN   2017/7/10     Fully Paid\n8      A        A5  10+ years       MORTGAGE   2017/9/11     Fully Paid\n18     A        A3  10+ years       MORTGAGE   2017/4/11     Fully Paid\n65     A        A2    2 years       MORTGAGE   2017/11/9     Fully Paid\n64     A        A4    5 years       MORTGAGE   2017/11/9     Fully Paid\n19     A        A5     1 year       MORTGAGE   2017/4/11     Fully Paid\n25     A        A5    8 years       MORTGAGE   2017/4/11     Fully Paid\n29     A        A4  10+ years           RENT   2017/1/11     Fully Paid\n28     A        A5    8 years       MORTGAGE   2017/4/11     Fully Paid\n43     A        A5    8 years       MORTGAGE   2017/7/10     Fully Paid\n3      A        A4  10+ years           RENT  2017/11/11     fully Paid\n31     A        A5     1 year           RENT   2017/1/11     Fully Paid\n54     A        A5    8 years           RENT   2017/5/10     Fully Paid\n32     A        A4    9 years            OWN   2017/1/11    Charged Off\n38     A        A5  10+ years       MORTGAGE  2017/10/10     Fully Paid\n49     A        A5    8 years           RENT   2017/5/10     Fully Paid\n41     A        A3  10+ years            OWN  2017/10/10     fully Paid\n\n[68 rows x 6 columns]\n"
     ]
    }
   ],
   "source": [
    "print(loan_object.sort_values('grade',ascending=False))# 按照指定列排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4 汇总及描述性统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(68, 11)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data.shape# 数据框的行列数 shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   loan_amnt grade sub_grade emp_length home_ownership  annual_inc  \\\n",
      "0     5000.0     B        B2  10+ years           RENT     24000.0   \n",
      "\n",
      "      issue_d loan_status  open_acc  total_pymnt  total_rec_int  \n",
      "0  2017/12/11  Fully Paid         3  5863.155187         863.16  \n",
      "    loan_amnt grade sub_grade emp_length home_ownership  annual_inc  \\\n",
      "67    17600.0     D        D4    8 years           RENT     72000.0   \n",
      "\n",
      "      issue_d loan_status  open_acc  total_pymnt  total_rec_int  \n",
      "67  2017/5/11  Fully Paid        14  20172.78686        2572.79  \n"
     ]
    }
   ],
   "source": [
    "print(loan_data.head(1))# 数据框的前几行，后几行\n",
    "print(loan_data.tail(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B    18\nA    17\nC    14\nD    11\nE     6\nF     2\nName: grade, dtype: int64\n<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "source": [
    "r = loan_data['grade'].value_counts()# Series 的唯一值个数 value_counts\n",
    "print(r)\n",
    "print(type(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 68 entries, 0 to 67\n",
      "Data columns (total 11 columns):\n",
      "loan_amnt         66 non-null float64\n",
      "grade             68 non-null object\n",
      "sub_grade         68 non-null object\n",
      "emp_length        68 non-null object\n",
      "home_ownership    68 non-null object\n",
      "annual_inc        65 non-null float64\n",
      "issue_d           68 non-null object\n",
      "loan_status       68 non-null object\n",
      "open_acc          68 non-null int64\n",
      "total_pymnt       68 non-null float64\n",
      "total_rec_int     68 non-null float64\n",
      "dtypes: float64(4), int64(1), object(6)\n",
      "memory usage: 5.9+ KB\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(loan_data.info()) # 数据框信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           loan_amnt     annual_inc   open_acc   total_pymnt  total_rec_int\ncount      66.000000      65.000000  68.000000     68.000000      68.000000\nmean    19913.257576   73199.676923   9.264706  14637.408621    2965.349559\nstd     60650.680480   65755.454700   3.815200  11882.493935    3569.197500\nmin      1800.000000   16000.000000   2.000000   1014.530000     203.350000\n25%      6000.000000   45600.000000   7.000000   6543.590000     988.145000\n50%     11000.000000   63000.000000   9.000000  10819.285650    1612.175000\n75%     17600.000000   82000.000000  11.250000  19901.630490    3046.635000\nmax    500000.000000  550000.000000  19.000000  48845.292210   14124.860000\n"
     ]
    }
   ],
   "source": [
    "print(loan_data.describe()) # 描述性统计信息汇总"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                  loan_amnt    annual_inc      open_acc   total_pymnt  \\\nloan_amnt      3.678505e+09 -3.274485e+07 -19247.109557  1.396036e+08   \nannual_inc    -3.274485e+07  4.323780e+09  -8030.857212  1.537413e+08   \nopen_acc      -1.924711e+04 -8.030857e+03     14.555751  6.464778e+03   \ntotal_pymnt    1.396036e+08  1.537413e+08   6464.778351  1.411937e+08   \ntotal_rec_int  2.674619e+07  3.458975e+07   1693.108477  3.868748e+07   \n\n               total_rec_int  \nloan_amnt       2.674619e+07  \nannual_inc      3.458975e+07  \nopen_acc        1.693108e+03  \ntotal_pymnt     3.868748e+07  \ntotal_rec_int   1.273917e+07  \n"
     ]
    }
   ],
   "source": [
    "print(loan_data.cov()) # 协方差矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               loan_amnt  annual_inc  open_acc  total_pymnt  total_rec_int\nloan_amnt       1.000000   -0.007904 -0.082313     0.191863       0.122136\nannual_inc     -0.007904    1.000000 -0.032187     0.193778       0.144707\nopen_acc       -0.082313   -0.032187  1.000000     0.142603       0.124336\ntotal_pymnt     0.191863    0.193778  0.142603     1.000000       0.912205\ntotal_rec_int   0.122136    0.144707  0.124336     0.912205       1.000000\n"
     ]
    }
   ],
   "source": [
    "print(loan_data.corr())# 相关系数矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5 文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "loan_skip10 = pd.read_csv('./input/loandata.csv',skiprows=10) # 跳过前多少行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "loan_10 = pd.read_csv('./input/loandata.csv',nrows=10)# 指定行数 nrows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "loan_10_encode = pd.read_csv('./input/loandata.csv',nrows=10,encoding='utf-8')# 指定行数 nrows以及编码方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8.3271484375\n8.330078125\n8.2685546875\n3.3740234375\n"
     ]
    }
   ],
   "source": [
    "# 中等数量级数据读写及操作 chunksize\n",
    "chunker = pd.read_csv('./data/loandata.csv',chunksize=20)\n",
    "for chunk in chunker:\n",
    "    print(sum(chunk.memory_usage(deep='True'))/1024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index              80\nloan_amnt         544\ngrade             544\nsub_grade         544\nemp_length        544\nhome_ownership    544\nannual_inc        544\nissue_d           544\nloan_status       544\nopen_acc          544\ntotal_pymnt       544\ntotal_rec_int     544\ndtype: int64\n5.921875\n"
     ]
    }
   ],
   "source": [
    "print(loan_data.memory_usage())# 文件占用内存情况分析 \n",
    "print(sum(loan_data.memory_usage())/1024)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index               72\n",
      "loan_amnt          544\n",
      "grade             3128\n",
      "sub_grade         3196\n",
      "emp_length        3581\n",
      "home_ownership    3441\n",
      "annual_inc         544\n",
      "issue_d           3682\n",
      "loan_status       3759\n",
      "open_acc           544\n",
      "total_pymnt        544\n",
      "total_rec_int      544\n",
      "dtype: int64\n",
      "23\n"
     ]
    }
   ],
   "source": [
    "print(loan_data.memory_usage(deep=True)# 文件占用内存情况分析 deep\n",
    "print(sum(loan_data.memory_usage(deep=True))/1024"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用字典的方式指定某一列的特定值为缺失NaN，使得能够识别\n",
    "map_id = {'grade':'A'}\n",
    "loan_A_miss = pd.read_csv('./input/loandata.csv',na_values=map_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6 ndarray、Series和DataFrame的相互转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    2\n",
       "B    3\n",
       "C    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "array_1d = np.array([2,3,4])\n",
    "ser_1d = pd.Series(array_1d, index=list('ABC')) # 一维array转换成Series\n",
    "print(ser_1d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>11</td>\n",
       "      <td>12</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C\n",
       "a   2   3   4\n",
       "b  11  12  13"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "array_2d = np.array([[2,3,4],[11,12,13]])#二维array转换成DataFrame\n",
    "df_2d = pd.DataFrame(array_2d,index=['a','b'],columns=['A','B','C'])\n",
    "df_2d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a\n",
       "A  2\n",
       "B  3\n",
       "C  4"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(ser_1d,columns=['a']) # Series转换成DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4]\n",
      " [11 12 13]]\n",
      "[[ 2  3  4]\n",
      " [11 12 13]]\n"
     ]
    }
   ],
   "source": [
    "print(np.array(df_2d))# DataFrame转换成ndarray\n",
    "print(df_2d.values)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7 特征离散化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6000.0      6\n8000.0      6\n12000.0     5\n5000.0      4\n4000.0      4\n25000.0     3\n35000.0     3\n2000.0      2\n30000.0     2\n14000.0     2\n7000.0      2\n1800.0      2\n17600.0     2\n18000.0     2\n20000.0     2\n12400.0     1\n2500.0      1\n16000.0     1\n24175.0     1\n500000.0    1\n9000.0      1\n13000.0     1\n15000.0     1\n12800.0     1\n2100.0      1\n7200.0      1\n6600.0      1\n27000.0     1\n17325.0     1\n12500.0     1\n24250.0     1\n14125.0     1\n7500.0      1\n10000.0     1\nName: loan_amnt, dtype: int64\n0       d\n1       c\n2       e\n3       e\n4       e\n5       e\n6       e\n7       d\n8       d\n9       e\n10      c\n11      d\n12      e\n13      e\n14      e\n15      e\n16      e\n17      d\n18      e\n19      c\n20      e\n21      d\n22      e\n23      e\n24      e\n25      e\n26      e\n27      e\n28      e\n29    NaN\n     ... \n38      e\n39      d\n40      d\n41      e\n42      d\n43      d\n44      e\n45    NaN\n46      b\n47      e\n48      b\n49      d\n50      d\n51      e\n52      c\n53      b\n54      d\n55      d\n56    NaN\n57      c\n58      d\n59      e\n60      c\n61      e\n62      d\n63      e\n64      d\n65      b\n66      d\n67      e\nName: loan_amnt, Length: 68, dtype: category\nCategories (5, object): [a < b < c < d < e]\n"
     ]
    }
   ],
   "source": [
    "amnt_series = loan_data['loan_amnt']\n",
    "# 指定区间长度的离散化\n",
    "print(amnt_series.value_counts())\n",
    "\n",
    "bins = [0, 1000, 2000, 4000, 8000, 100000]\n",
    "cut = pd.cut(amnt_series, bins, labels=['a', 'b', 'c', 'd', 'e'])\n",
    "print(cut)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    20\nc    17\nd    16\nb    13\nName: loan_amnt, dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 等频离散化\n",
    "cuts = pd.qcut(amnt_series,4,labels=['a','b','c','d'])\n",
    "cuts.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8 缺失值填补与离群值处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 8.1 查看数据集的各个属性是否存在缺失值，并统计缺失值数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    loan_amnt  grade  sub_grade  emp_length  home_ownership  annual_inc  \\\n0        True   True       True        True            True        True   \n1        True   True       True        True            True        True   \n2        True   True       True        True            True        True   \n3        True   True       True        True            True        True   \n4        True   True       True        True            True        True   \n5        True   True       True        True            True       False   \n6        True   True       True        True            True        True   \n7        True   True       True        True            True        True   \n8        True   True       True        True            True        True   \n9        True   True       True        True            True        True   \n10       True   True       True        True            True        True   \n11       True   True       True        True            True        True   \n12       True   True       True        True            True        True   \n13       True   True       True        True            True        True   \n14       True   True       True        True            True        True   \n15       True   True       True        True            True        True   \n16       True   True       True        True            True        True   \n17       True   True       True        True            True        True   \n18       True   True       True        True            True        True   \n19       True   True       True        True            True        True   \n20       True   True       True        True            True        True   \n21       True   True       True        True            True        True   \n22       True   True       True        True            True        True   \n23       True   True       True        True            True        True   \n24       True   True       True        True            True        True   \n25       True   True       True        True            True        True   \n26       True   True       True        True            True        True   \n27       True   True       True        True            True        True   \n28       True   True       True        True            True        True   \n29      False   True       True        True            True        True   \n..        ...    ...        ...         ...             ...         ...   \n38       True   True       True        True            True        True   \n39       True   True       True        True            True        True   \n40       True   True       True        True            True        True   \n41       True   True       True        True            True        True   \n42       True   True       True        True            True        True   \n43       True   True       True        True            True        True   \n44       True   True       True        True            True        True   \n45      False   True       True        True            True        True   \n46       True   True       True       False            True        True   \n47       True   True       True        True            True        True   \n48       True   True       True        True            True        True   \n49       True   True       True        True            True        True   \n50       True   True       True        True            True        True   \n51       True   True       True        True            True        True   \n52       True   True       True        True            True        True   \n53       True   True       True        True            True        True   \n54       True   True       True        True            True        True   \n55       True   True       True        True            True        True   \n56       True   True       True        True            True        True   \n57       True   True       True        True            True        True   \n58       True   True       True        True            True        True   \n59       True   True       True        True            True        True   \n60       True   True       True        True            True       False   \n61       True   True       True        True            True        True   \n62       True   True       True        True            True        True   \n63       True   True       True        True            True        True   \n64       True   True       True        True            True        True   \n65       True   True       True        True            True        True   \n66       True   True       True        True            True        True   \n67       True   True       True        True            True        True   \n\n    issue_d  loan_status  open_acc  total_pymnt  total_rec_int  \n0      True         True      True         True           True  \n1      True         True      True         True           True  \n2      True         True      True         True           True  \n3      True         True      True         True           True  \n4      True         True      True         True           True  \n5      True         True      True         True           True  \n6      True         True      True         True           True  \n7      True         True      True         True           True  \n8      True         True      True         True           True  \n9      True         True      True         True           True  \n10     True         True      True         True           True  \n11     True         True      True         True           True  \n12     True         True      True         True           True  \n13     True         True      True         True           True  \n14     True         True      True         True           True  \n15     True         True      True         True           True  \n16     True         True      True         True           True  \n17     True         True      True         True           True  \n18     True         True      True         True           True  \n19     True         True      True         True           True  \n20     True         True      True         True           True  \n21     True         True      True         True           True  \n22     True         True      True         True           True  \n23     True         True      True         True           True  \n24     True         True      True         True           True  \n25     True         True      True         True           True  \n26     True         True      True         True           True  \n27     True         True      True         True           True  \n28     True         True      True         True           True  \n29     True         True      True         True           True  \n..      ...          ...       ...          ...            ...  \n38     True         True      True         True           True  \n39     True         True      True         True           True  \n40     True         True      True         True           True  \n41     True         True      True         True           True  \n42     True         True      True         True           True  \n43     True         True      True         True           True  \n44     True         True      True         True           True  \n45     True         True      True         True           True  \n46     True         True      True         True           True  \n47     True         True      True         True           True  \n48     True         True      True         True           True  \n49     True         True      True         True           True  \n50     True         True      True         True           True  \n51     True         True      True         True           True  \n52     True         True      True         True           True  \n53     True         True      True         True           True  \n54     True         True      True         True           True  \n55     True         True      True         True           True  \n56     True         True      True         True           True  \n57     True         True      True         True           True  \n58     True         True      True         True           True  \n59     True         True      True         True           True  \n60     True         True      True         True           True  \n61     True         True      True         True           True  \n62     True         True      True         True           True  \n63     True         True      True         True           True  \n64     True         True      True         True           True  \n65     True         True      True         True           True  \n66     True         True      True         True           True  \n67     True         True      True         True           True  \n\n[68 rows x 11 columns]\n"
     ]
    }
   ],
   "source": [
    "print(loan_data.isnull()==False)# 判断每一个数据是否为缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "loan_amnt          True\ngrade             False\nsub_grade         False\nemp_length         True\nhome_ownership    False\nannual_inc         True\nissue_d           False\nloan_status       False\nopen_acc          False\ntotal_pymnt       False\ntotal_rec_int     False\ndtype: bool"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data.isnull().any() # 存在缺失值的列是那些"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "loan_amnt         2\n",
       "grade             0\n",
       "sub_grade         0\n",
       "emp_length        0\n",
       "home_ownership    0\n",
       "annual_inc        3\n",
       "issue_d           0\n",
       "loan_status       0\n",
       "open_acc          0\n",
       "total_pymnt       0\n",
       "total_rec_int     0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(loan_data.isnull(), axis=0)  # 统计每一行缺失值的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False,  True, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [ True, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False,  True, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [ True, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False,  True, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False,  True, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False],\n       [False, False, False, False, False, False, False, False, False,\n        False, False]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data.isnull().values == True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    loan_amnt grade sub_grade emp_length home_ownership  annual_inc  \\\n5     16000.0     C        C2  10+ years            OWN         NaN   \n29        NaN     A        A4  10+ years           RENT     58000.0   \n36    10000.0     C        C2    2 years       MORTGAGE         NaN   \n45        NaN     B        B5    8 years       MORTGAGE     62400.0   \n46     2000.0     A        A1        NaN            OWN     16000.0   \n60     4000.0     B        B2    2 years           RENT         NaN   \n\n       issue_d  loan_status  open_acc   total_pymnt  total_rec_int  \n5   2017/10/11  Charged Off        13   9202.000000        3800.05  \n29   2017/1/11   Fully Paid        12  10994.614810         994.61  \n36   2017/9/10   Fully Paid        10  13181.651550        3181.65  \n45   2017/8/10  Charged Off         8   5207.370000        1520.88  \n46   2017/7/10   Fully Paid         9   2203.348430         203.35  \n60   2017/5/10   Fully Paid         3   4318.218537         318.22  \n"
     ]
    }
   ],
   "source": [
    "print(loan_data[loan_data.isnull().values]) # 展示存在缺失值的行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 8.2 删除缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    loan_amnt grade sub_grade emp_length home_ownership  annual_inc  \\\n0      5000.0     B        B2  10+ years           RENT     24000.0   \n1      2500.0     C        C4   < 1 year           RENT     30000.0   \n2     12500.0     D        D4  10+ years           RENT     74400.0   \n3     14000.0     A        A4  10+ years           RENT     72000.0   \n4     35000.0     F        F5    4 years       MORTGAGE    100000.0   \n6     14125.0     B        B5  10+ years            OWN     60000.0   \n7      5000.0     B        B2    4 years           RENT     49000.0   \n8      6000.0     A        A5  10+ years       MORTGAGE     48000.0   \n9     12000.0     C        C3   < 1 year       MORTGAGE     81996.0   \n10     4000.0     D        D4  10+ years       MORTGAGE     40000.0   \n11     8000.0     B        B4     1 year           RENT     42000.0   \n12    27000.0     D        D5    6 years       MORTGAGE     83004.0   \n13    25000.0     C        C4  10+ years           RENT     79000.0   \n14    17600.0     D        D4    8 years           RENT     72000.0   \n15    20000.0     D        D2    4 years           RENT     40000.0   \n16    12400.0     C        C2  10+ years           RENT     65000.0   \n17     6000.0     D        D3   < 1 year       MORTGAGE     60000.0   \n18    12800.0     A        A3  10+ years       MORTGAGE    100000.0   \n19     2100.0     A        A5     1 year       MORTGAGE     72000.0   \n20    17325.0     F        F2    9 years           RENT     35004.0   \n21     5000.0     C        C5  10+ years       MORTGAGE     69000.0   \n22    24175.0     B        B5    9 years       MORTGAGE     95000.0   \n23    30000.0     E        E1     1 year       MORTGAGE    100000.0   \n24    35000.0     E        E1  10+ years           RENT    106000.0   \n25    18000.0     A        A5    8 years       MORTGAGE     82000.0   \n26    30000.0     E        E1     1 year       MORTGAGE    100000.0   \n27    35000.0     E        E1  10+ years           RENT    106000.0   \n28    18000.0     A        A5    8 years       MORTGAGE     82000.0   \n30    12000.0     C        C2  10+ years       MORTGAGE     75000.0   \n31    12000.0     A        A5     1 year           RENT    105000.0   \n..        ...   ...       ...        ...            ...         ...   \n34     8000.0     D        D5   < 1 year           RENT     41600.0   \n35    12000.0     C        C1  10+ years           RENT     96000.0   \n37     6000.0     D        D3   < 1 year       MORTGAGE     60000.0   \n38    15000.0     A        A5  10+ years       MORTGAGE     55000.0   \n39     8000.0     B        B2    5 years       MORTGAGE    550000.0   \n40     7200.0     C        C5  10+ years           RENT     68400.0   \n41    14000.0     A        A3  10+ years            OWN     71000.0   \n42     7500.0     B        B5     1 year           RENT     35004.0   \n43     6600.0     A        A5    8 years       MORTGAGE     55800.0   \n44    12000.0     C        C1    5 years           RENT     45600.0   \n47    13000.0     D        D4  10+ years       MORTGAGE     25000.0   \n48     1800.0     C        C3    6 years            OWN     45600.0   \n49     6000.0     A        A5    8 years           RENT     82000.0   \n50     7000.0     B        B3  10+ years       MORTGAGE    102660.0   \n51    25000.0     E        E1  10+ years           RENT     56000.0   \n52     4000.0     B        B2    2 years           RENT     16800.0   \n53     1800.0     C        C3    6 years            OWN     45600.0   \n54     6000.0     A        A5    8 years           RENT     82000.0   \n55     7000.0     B        B3  10+ years       MORTGAGE    102660.0   \n56   500000.0     E        E1  10+ years           RENT     56000.0   \n57     4000.0     B        B2    2 years           RENT     16800.0   \n58     6000.0     D        D2    2 years           RENT     48000.0   \n59    25000.0     B        B4    6 years           RENT     60000.0   \n61    24250.0     B        B5  10+ years       MORTGAGE    140000.0   \n62     8000.0     B        B5    2 years           RENT     63000.0   \n63     9000.0     B        B1    5 years       MORTGAGE     54000.0   \n64     8000.0     A        A4    5 years       MORTGAGE     38000.0   \n65     2000.0     A        A2    2 years       MORTGAGE     44000.0   \n66     8000.0     B        B5    2 years           RENT     63000.0   \n67    17600.0     D        D4    8 years           RENT     72000.0   \n\n       issue_d    loan_status  open_acc   total_pymnt  total_rec_int  \n0   2017/12/11     Fully Paid         3   5863.155187         863.16  \n1   2017/12/11    Charged Off         3   1014.530000         435.17  \n2   2017/11/11     Fully Paid         8  14722.411910        2222.41  \n3   2017/11/11     fully Paid         6  15099.982530        1099.98  \n4   2017/10/11     Fully Paid         9  48743.119270       13743.12  \n6   2017/10/11     Fully Paid        13  16750.738520        2625.74  \n7    2017/9/11     Fully Paid        10   5863.155187         863.16  \n8    2017/9/11     Fully Paid        11   7235.598134        1235.60  \n9   2017/10/11     Fully Paid        11  16996.739990        4996.74  \n10   2017/6/11     Fully Paid         3   5118.676867        1118.68  \n11   2017/6/11    Charged Off        10   3962.340000         968.75  \n12   2017/6/11     Fully Paid         7  29820.276860        2820.28  \n13   2017/5/11     Fully Paid        13  33063.912780        8063.91  \n14   2017/5/11     Fully Paid        14  20172.786860        2572.79  \n15   2017/5/11    Charged Off         6  23742.300000        8046.39  \n16   2017/5/11     Fully Paid        10  15146.533740        2746.53  \n17   2017/5/11    Charged Off         7   6543.590000        1584.75  \n18   2017/4/11     Fully Paid        10  14211.312910        1411.31  \n19   2017/4/11     Fully Paid        15   2357.178487         257.18  \n20   2017/4/11    Charged Off        10   7437.120000        3758.96  \n21   2017/3/11     Fully Paid         7   6183.766196        1168.77  \n22   2017/3/11    charged Off         8  24422.870000        4242.70  \n23   2017/4/11     Fully Paid        14  44124.859950       14124.86  \n24   2017/3/11     Fully Paid         8  48845.292210       13845.29  \n25   2017/4/11     Fully Paid         8  20204.461240        2204.46  \n26   2017/4/11     Fully Paid        14  44124.859950       14124.86  \n27   2017/3/11     Fully Paid         8  48845.292210       13845.29  \n28   2017/4/11     Fully Paid         8  20204.461240        2204.46  \n30   2017/1/11     Fully Paid        15  14475.294480        2475.29  \n31   2017/1/11     Fully Paid        10  13041.191490        1041.19  \n..         ...            ...       ...           ...            ...  \n34   2017/1/11     Fully Paid         6  11557.640000        3557.64  \n35   2017/9/10     fully Paid         8  14410.935340        2410.94  \n37   2017/5/11    Charged Off         7   6543.590000        1584.75  \n38  2017/10/10     Fully Paid        14  16274.762910        1274.76  \n39   2017/9/10     Fully Paid         5   9394.916746        1394.92  \n40   2017/9/10     Fully Paid         9   8844.469278        1644.47  \n41  2017/10/10     fully Paid         8  15595.389910        1595.39  \n42   2017/9/10     Fully Paid         6   9978.050264        2478.05  \n43   2017/7/10     Fully Paid         8   7349.816860         749.82  \n44   2017/7/10     Fully Paid         7  13565.703560        1565.70  \n47   2017/7/10     fully Paid        19  18442.068880        5442.07  \n48   2017/5/10     Fully Paid        11   2025.550714         225.55  \n49   2017/5/10     Fully Paid        17   6745.258045         745.26  \n50   2017/5/10     Fully Paid         9   9052.310001        2052.31  \n51   2017/5/10     Fully Paid         6  19901.630490        3001.63  \n52   2017/5/10     Fully Paid         3   4318.218537         318.22  \n53   2017/5/10     Fully Paid        11   2025.550714         225.55  \n54   2017/5/10     Fully Paid        17   6745.258045         745.26  \n55   2017/5/10     fully Paid         9   9052.310001        2052.31  \n56   2017/5/10     Fully Paid         6  19901.630490        3001.63  \n57   2017/5/10     Fully Paid         3   4318.218537         318.22  \n58   2017/4/10     Fully Paid         7   7463.352797        1463.35  \n59   2017/4/10     Fully Paid        11  29462.617620        4462.62  \n61   2017/12/9     Fully Paid        15  27565.431850        3315.44  \n62   2017/12/9     Fully Paid         7   9171.043721        1171.04  \n63   2017/11/9     Fully Paid        12  10643.956490        1628.96  \n64   2017/11/9     Fully Paid         6   8322.639678         322.64  \n65   2017/11/9     Fully Paid        11   2207.212632         207.21  \n66   2017/12/9     Fully Paid         7   9171.043721        1171.04  \n67   2017/5/11     Fully Paid        14  20172.786860        2572.79  \n\n[62 rows x 11 columns]\n"
     ]
    }
   ],
   "source": [
    "from copy import deepcopy\n",
    "loan_data_miss = deepcopy(loan_data)\n",
    "#print(loan_data_miss)\n",
    "# 舍弃缺失值 dropna 并指定方法 how   ,any :只要有一列为None就删除这一行数据\n",
    "print(loan_data_miss.dropna(how='any',axis=0))\n",
    "\n",
    "# inplace=True 在原DataFrame上做更改\n",
    "loan_data_miss.dropna(how='any',inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 8.3 填补缺失值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   loan_amnt grade sub_grade emp_length home_ownership annual_inc     issue_d  \\\n0       5000     B        B2  10+ years           RENT      24000  2017/12/11   \n1       2500     C        C4   < 1 year           RENT      30000  2017/12/11   \n2      12500     D        D4  10+ years           RENT      74400  2017/11/11   \n3      14000     A        A4  10+ years           RENT      72000  2017/11/11   \n4      35000     F        F5    4 years       MORTGAGE     100000  2017/10/11   \n5      16000     C        C2  10+ years            OWN        缺失值  2017/10/11   \n6      14125     B        B5  10+ years            OWN      60000  2017/10/11   \n7       5000     B        B2    4 years           RENT      49000   2017/9/11   \n8       6000     A        A5  10+ years       MORTGAGE      48000   2017/9/11   \n9      12000     C        C3   < 1 year       MORTGAGE      81996  2017/10/11   \n10      4000     D        D4  10+ years       MORTGAGE      40000   2017/6/11   \n11      8000     B        B4     1 year           RENT      42000   2017/6/11   \n12     27000     D        D5    6 years       MORTGAGE      83004   2017/6/11   \n13     25000     C        C4  10+ years           RENT      79000   2017/5/11   \n14     17600     D        D4    8 years           RENT      72000   2017/5/11   \n15     20000     D        D2    4 years           RENT      40000   2017/5/11   \n16     12400     C        C2  10+ years           RENT      65000   2017/5/11   \n17      6000     D        D3   < 1 year       MORTGAGE      60000   2017/5/11   \n18     12800     A        A3  10+ years       MORTGAGE     100000   2017/4/11   \n19      2100     A        A5     1 year       MORTGAGE      72000   2017/4/11   \n20     17325     F        F2    9 years           RENT      35004   2017/4/11   \n21      5000     C        C5  10+ years       MORTGAGE      69000   2017/3/11   \n22     24175     B        B5    9 years       MORTGAGE      95000   2017/3/11   \n23     30000     E        E1     1 year       MORTGAGE     100000   2017/4/11   \n24     35000     E        E1  10+ years           RENT     106000   2017/3/11   \n25     18000     A        A5    8 years       MORTGAGE      82000   2017/4/11   \n26     30000     E        E1     1 year       MORTGAGE     100000   2017/4/11   \n27     35000     E        E1  10+ years           RENT     106000   2017/3/11   \n28     18000     A        A5    8 years       MORTGAGE      82000   2017/4/11   \n29       缺失值     A        A4  10+ years           RENT      58000   2017/1/11   \n..       ...   ...       ...        ...            ...        ...         ...   \n38     15000     A        A5  10+ years       MORTGAGE      55000  2017/10/10   \n39      8000     B        B2    5 years       MORTGAGE     550000   2017/9/10   \n40      7200     C        C5  10+ years           RENT      68400   2017/9/10   \n41     14000     A        A3  10+ years            OWN      71000  2017/10/10   \n42      7500     B        B5     1 year           RENT      35004   2017/9/10   \n43      6600     A        A5    8 years       MORTGAGE      55800   2017/7/10   \n44     12000     C        C1    5 years           RENT      45600   2017/7/10   \n45       缺失值     B        B5    8 years       MORTGAGE      62400   2017/8/10   \n46      2000     A        A1        NaN            OWN      16000   2017/7/10   \n47     13000     D        D4  10+ years       MORTGAGE      25000   2017/7/10   \n48      1800     C        C3    6 years            OWN      45600   2017/5/10   \n49      6000     A        A5    8 years           RENT      82000   2017/5/10   \n50      7000     B        B3  10+ years       MORTGAGE     102660   2017/5/10   \n51     25000     E        E1  10+ years           RENT      56000   2017/5/10   \n52      4000     B        B2    2 years           RENT      16800   2017/5/10   \n53      1800     C        C3    6 years            OWN      45600   2017/5/10   \n54      6000     A        A5    8 years           RENT      82000   2017/5/10   \n55      7000     B        B3  10+ years       MORTGAGE     102660   2017/5/10   \n56    500000     E        E1  10+ years           RENT      56000   2017/5/10   \n57      4000     B        B2    2 years           RENT      16800   2017/5/10   \n58      6000     D        D2    2 years           RENT      48000   2017/4/10   \n59     25000     B        B4    6 years           RENT      60000   2017/4/10   \n60      4000     B        B2    2 years           RENT        缺失值   2017/5/10   \n61     24250     B        B5  10+ years       MORTGAGE     140000   2017/12/9   \n62      8000     B        B5    2 years           RENT      63000   2017/12/9   \n63      9000     B        B1    5 years       MORTGAGE      54000   2017/11/9   \n64      8000     A        A4    5 years       MORTGAGE      38000   2017/11/9   \n65      2000     A        A2    2 years       MORTGAGE      44000   2017/11/9   \n66      8000     B        B5    2 years           RENT      63000   2017/12/9   \n67     17600     D        D4    8 years           RENT      72000   2017/5/11   \n\n      loan_status  open_acc   total_pymnt  total_rec_int  \n0      Fully Paid         3   5863.155187         863.16  \n1     Charged Off         3   1014.530000         435.17  \n2      Fully Paid         8  14722.411910        2222.41  \n3      fully Paid         6  15099.982530        1099.98  \n4      Fully Paid         9  48743.119270       13743.12  \n5     Charged Off        13   9202.000000        3800.05  \n6      Fully Paid        13  16750.738520        2625.74  \n7      Fully Paid        10   5863.155187         863.16  \n8      Fully Paid        11   7235.598134        1235.60  \n9      Fully Paid        11  16996.739990        4996.74  \n10     Fully Paid         3   5118.676867        1118.68  \n11    Charged Off        10   3962.340000         968.75  \n12     Fully Paid         7  29820.276860        2820.28  \n13     Fully Paid        13  33063.912780        8063.91  \n14     Fully Paid        14  20172.786860        2572.79  \n15    Charged Off         6  23742.300000        8046.39  \n16     Fully Paid        10  15146.533740        2746.53  \n17    Charged Off         7   6543.590000        1584.75  \n18     Fully Paid        10  14211.312910        1411.31  \n19     Fully Paid        15   2357.178487         257.18  \n20    Charged Off        10   7437.120000        3758.96  \n21     Fully Paid         7   6183.766196        1168.77  \n22    charged Off         8  24422.870000        4242.70  \n23     Fully Paid        14  44124.859950       14124.86  \n24     Fully Paid         8  48845.292210       13845.29  \n25     Fully Paid         8  20204.461240        2204.46  \n26     Fully Paid        14  44124.859950       14124.86  \n27     Fully Paid         8  48845.292210       13845.29  \n28     Fully Paid         8  20204.461240        2204.46  \n29     Fully Paid        12  10994.614810         994.61  \n..            ...       ...           ...            ...  \n38     Fully Paid        14  16274.762910        1274.76  \n39     Fully Paid         5   9394.916746        1394.92  \n40     Fully Paid         9   8844.469278        1644.47  \n41     fully Paid         8  15595.389910        1595.39  \n42     Fully Paid         6   9978.050264        2478.05  \n43     Fully Paid         8   7349.816860         749.82  \n44     Fully Paid         7  13565.703560        1565.70  \n45    Charged Off         8   5207.370000        1520.88  \n46     Fully Paid         9   2203.348430         203.35  \n47     fully Paid        19  18442.068880        5442.07  \n48     Fully Paid        11   2025.550714         225.55  \n49     Fully Paid        17   6745.258045         745.26  \n50     Fully Paid         9   9052.310001        2052.31  \n51     Fully Paid         6  19901.630490        3001.63  \n52     Fully Paid         3   4318.218537         318.22  \n53     Fully Paid        11   2025.550714         225.55  \n54     Fully Paid        17   6745.258045         745.26  \n55     fully Paid         9   9052.310001        2052.31  \n56     Fully Paid         6  19901.630490        3001.63  \n57     Fully Paid         3   4318.218537         318.22  \n58     Fully Paid         7   7463.352797        1463.35  \n59     Fully Paid        11  29462.617620        4462.62  \n60     Fully Paid         3   4318.218537         318.22  \n61     Fully Paid        15  27565.431850        3315.44  \n62     Fully Paid         7   9171.043721        1171.04  \n63     Fully Paid        12  10643.956490        1628.96  \n64     Fully Paid         6   8322.639678         322.64  \n65     Fully Paid        11   2207.212632         207.21  \n66     Fully Paid         7   9171.043721        1171.04  \n67     Fully Paid        14  20172.786860        2572.79  \n\n[68 rows x 11 columns]\n"
     ]
    }
   ],
   "source": [
    "# 填充缺失值 fillna\n",
    "\n",
    "# 通过字典指定每一列的填充方式\n",
    "loan_data_fill = loan_data.fillna({'loan_amnt':'缺失值','annual_inc':'缺失值'})\n",
    "print(loan_data_fill)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "针对属性`loan_amount`\n",
    "\n",
    "+ 使用`total_pymnt`属性和total_tec_int属性的差值作为要填补的值。除了利息以外还可能包括逾期费，手续费和罚息等费用，只能近似获得一个实际贷款的金额，并对填补的值作格式转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "loan_data['loan_amnt'] = loan_data['loan_amnt'].fillna(loan_data['total_pymnt'] - loan_data['total_rec_int']).astype('int')\n",
    "loan_data.to_csv(\"data/b.csv\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "针对属性`annual_inc`\n",
    "\n",
    "+ 在数据集中没有其他属性作参考，选择用现有数据的均值进行填补"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "mean_val = loan_data['annual_inc'].mean()\n",
    "loan_data['annual_inc'] = loan_data['annual_inc'].fillna(mean_val)\n",
    "loan_data.to_csv(\"data/b.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     0\n0  0.0\n1  NaN\n2  3.0\n3  2.0\n     0\n0  0.0\n1  0.0\n2  3.0\n3  2.0\n"
     ]
    }
   ],
   "source": [
    "example = pd.DataFrame([0,np.nan,3,2])\n",
    "print(example)\n",
    "example = example.fillna(method='ffill')# 前向或后向填补,时间序列场景\n",
    "print(example)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 8.4 离群值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "               count   mean    std    min    25%    50%    75%     max\nloan_amnt         68  19528  59782   1800   6000  10000  17600  500000\nannual_inc        68  73199  64266  16000  47400  64000  82000  550000\nopen_acc          68      9      3      2      7      9     11      19\ntotal_pymnt       68  14637  11882   1014   6543  10819  19901   48845\ntotal_rec_int     68   2965   3569    203    988   1612   3046   14124\n"
     ]
    }
   ],
   "source": [
    "print(loan_data.describe().astype(np.int64).T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_loan_data = loan_data.replace(500000,loan_data['loan_amnt'].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>loan_amnt</th>\n",
       "      <td>68</td>\n",
       "      <td>19528</td>\n",
       "      <td>59782</td>\n",
       "      <td>1800</td>\n",
       "      <td>6000</td>\n",
       "      <td>10000</td>\n",
       "      <td>17600</td>\n",
       "      <td>500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>annual_inc</th>\n",
       "      <td>68</td>\n",
       "      <td>73199</td>\n",
       "      <td>64266</td>\n",
       "      <td>16000</td>\n",
       "      <td>47400</td>\n",
       "      <td>64000</td>\n",
       "      <td>82000</td>\n",
       "      <td>550000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>open_acc</th>\n",
       "      <td>68</td>\n",
       "      <td>9</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>9</td>\n",
       "      <td>11</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>total_pymnt</th>\n",
       "      <td>68</td>\n",
       "      <td>14637</td>\n",
       "      <td>11882</td>\n",
       "      <td>1014</td>\n",
       "      <td>6543</td>\n",
       "      <td>10819</td>\n",
       "      <td>19901</td>\n",
       "      <td>48845</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>total_rec_int</th>\n",
       "      <td>68</td>\n",
       "      <td>2965</td>\n",
       "      <td>3569</td>\n",
       "      <td>203</td>\n",
       "      <td>988</td>\n",
       "      <td>1612</td>\n",
       "      <td>3046</td>\n",
       "      <td>14124</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               count   mean    std    min    25%    50%    75%     max\n",
       "loan_amnt         68  19528  59782   1800   6000  10000  17600  500000\n",
       "annual_inc        68  73199  64266  16000  47400  64000  82000  550000\n",
       "open_acc          68      9      3      2      7      9     11      19\n",
       "total_pymnt       68  14637  11882   1014   6543  10819  19901   48845\n",
       "total_rec_int     68   2965   3569    203    988   1612   3046   14124"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data.describe().astype(np.int64).T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 8.5 类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "loan_amnt                float64\n",
       "grade                     object\n",
       "sub_grade                 object\n",
       "emp_length                object\n",
       "home_ownership            object\n",
       "annual_inc               float64\n",
       "issue_d           datetime64[ns]\n",
       "loan_status               object\n",
       "open_acc                   int64\n",
       "total_pymnt              float64\n",
       "total_rec_int            float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data['issue_d']=pd.to_datetime(loan_data['issue_d']) # 转换为datetime类型\n",
    "loan_data.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 练习：对属性`loan_status`的取值，统计频次会发现：由于空格，相同的贷款状态被“误认为”是不同的类别，这会造成统计结果不可用。因此，需要解决属性中存在空格的问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fully Paid       48\n",
       "Charged Off       8\n",
       "fully Paid        5\n",
       " Fully Paid       4\n",
       "   Fully Paid     1\n",
       "charged Off       1\n",
       "  Charged Off     1\n",
       "Name: loan_status, dtype: int64"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data['loan_status'].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      Fully Paid\n1     Charged Off\n2      Fully Paid\n3      Fully Paid\n4      Fully Paid\n5     Charged Off\n6      Fully Paid\n7      Fully Paid\n8      Fully Paid\n9      Fully Paid\n10     Fully Paid\n11    Charged Off\n12     Fully Paid\n13     Fully Paid\n14     Fully Paid\n15    Charged Off\n16     Fully Paid\n17    Charged Off\n18     Fully Paid\n19     Fully Paid\n20    Charged Off\n21     Fully Paid\n22    Charged Off\n23     Fully Paid\n24     Fully Paid\n25     Fully Paid\n26     Fully Paid\n27     Fully Paid\n28     Fully Paid\n29     Fully Paid\n         ...     \n38     Fully Paid\n39     Fully Paid\n40     Fully Paid\n41     Fully Paid\n42     Fully Paid\n43     Fully Paid\n44     Fully Paid\n45    Charged Off\n46     Fully Paid\n47     Fully Paid\n48     Fully Paid\n49     Fully Paid\n50     Fully Paid\n51     Fully Paid\n52     Fully Paid\n53     Fully Paid\n54     Fully Paid\n55     Fully Paid\n56     Fully Paid\n57     Fully Paid\n58     Fully Paid\n59     Fully Paid\n60     Fully Paid\n61     Fully Paid\n62     Fully Paid\n63     Fully Paid\n64     Fully Paid\n65     Fully Paid\n66     Fully Paid\n67     Fully Paid\nName: loan_status, Length: 68, dtype: object\n0      Fully Paid\n1     Charged Off\n2      Fully Paid\n3      Fully Paid\n4      Fully Paid\n5     Charged Off\n6      Fully Paid\n7      Fully Paid\n8      Fully Paid\n9      Fully Paid\n10     Fully Paid\n11    Charged Off\n12     Fully Paid\n13     Fully Paid\n14     Fully Paid\n15    Charged Off\n16     Fully Paid\n17    Charged Off\n18     Fully Paid\n19     Fully Paid\n20    Charged Off\n21     Fully Paid\n22    Charged Off\n23     Fully Paid\n24     Fully Paid\n25     Fully Paid\n26     Fully Paid\n27     Fully Paid\n28     Fully Paid\n29     Fully Paid\n         ...     \n38     Fully Paid\n39     Fully Paid\n40     Fully Paid\n41     Fully Paid\n42     Fully Paid\n43     Fully Paid\n44     Fully Paid\n45    Charged Off\n46     Fully Paid\n47     Fully Paid\n48     Fully Paid\n49     Fully Paid\n50     Fully Paid\n51     Fully Paid\n52     Fully Paid\n53     Fully Paid\n54     Fully Paid\n55     Fully Paid\n56     Fully Paid\n57     Fully Paid\n58     Fully Paid\n59     Fully Paid\n60     Fully Paid\n61     Fully Paid\n62     Fully Paid\n63     Fully Paid\n64     Fully Paid\n65     Fully Paid\n66     Fully Paid\n67     Fully Paid\nName: loan_status, Length: 68, dtype: object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Fully Paid     58\nCharged Off    10\nName: loan_status, dtype: int64"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loan_data['loan_status'] = loan_data['loan_status'].map(str.strip) #q去除先后空格\n",
    "loan_data['loan_status'] = loan_data['loan_status'].map(str.title)\n",
    "loan_data['loan_status'].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9 数据融合和分组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# concat,merge 见cheatsheet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据开户数量的多少对用户进行分级，也就是对属性`open_acc`进行分组\n",
    "\n",
    "+ 6个账户以下为A级，6-12个账户为B级，12-18个账户为C级，18-24个账户为D级\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'new_loan_data' is not defined",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-81-b979e535c941>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mgroup_names\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'A'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'B'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'C'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'D'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mnew_loan_data\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'categories'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcut\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnew_loan_data\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'open_acc'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbins\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlabels\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mgroup_names\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'new_loan_data' is not defined"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "bins = [0, 6, 12, 18, 24]\n",
    "\n",
    "group_names = ['A', 'B', 'C', 'D']\n",
    "\n",
    "new_loan_data['categories'] = pd.cut(new_loan_data['open_acc'], bins, labels=group_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>loan_amnt</th>\n",
       "      <th>grade</th>\n",
       "      <th>sub_grade</th>\n",
       "      <th>emp_length</th>\n",
       "      <th>home_ownership</th>\n",
       "      <th>annual_inc</th>\n",
       "      <th>issue_d</th>\n",
       "      <th>loan_status</th>\n",
       "      <th>open_acc</th>\n",
       "      <th>total_pymnt</th>\n",
       "      <th>total_rec_int</th>\n",
       "      <th>categories</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B2</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>24000.000000</td>\n",
       "      <td>2017/12/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>3</td>\n",
       "      <td>5863.155187</td>\n",
       "      <td>863.16</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2500.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C4</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>RENT</td>\n",
       "      <td>30000.000000</td>\n",
       "      <td>2017/12/11</td>\n",
       "      <td>Charged Off</td>\n",
       "      <td>3</td>\n",
       "      <td>1014.530000</td>\n",
       "      <td>435.17</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>12500.000000</td>\n",
       "      <td>D</td>\n",
       "      <td>D4</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>74400.000000</td>\n",
       "      <td>2017/11/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>14722.411910</td>\n",
       "      <td>2222.41</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>14000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A4</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>72000.000000</td>\n",
       "      <td>2017/11/11</td>\n",
       "      <td>fully Paid</td>\n",
       "      <td>6</td>\n",
       "      <td>15099.982530</td>\n",
       "      <td>1099.98</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>35000.000000</td>\n",
       "      <td>F</td>\n",
       "      <td>F5</td>\n",
       "      <td>4 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>100000.000000</td>\n",
       "      <td>2017/10/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>9</td>\n",
       "      <td>48743.119270</td>\n",
       "      <td>13743.12</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>16000.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C2</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>OWN</td>\n",
       "      <td>73199.676923</td>\n",
       "      <td>2017/10/11</td>\n",
       "      <td>Charged Off</td>\n",
       "      <td>13</td>\n",
       "      <td>9202.000000</td>\n",
       "      <td>3800.05</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>14125.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B5</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>OWN</td>\n",
       "      <td>60000.000000</td>\n",
       "      <td>2017/10/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>13</td>\n",
       "      <td>16750.738520</td>\n",
       "      <td>2625.74</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B2</td>\n",
       "      <td>4 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>49000.000000</td>\n",
       "      <td>2017/9/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>10</td>\n",
       "      <td>5863.155187</td>\n",
       "      <td>863.16</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>6000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>48000.000000</td>\n",
       "      <td>2017/9/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>11</td>\n",
       "      <td>7235.598134</td>\n",
       "      <td>1235.60</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>12000.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C3</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>81996.000000</td>\n",
       "      <td>2017/10/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>11</td>\n",
       "      <td>16996.739990</td>\n",
       "      <td>4996.74</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>4000.000000</td>\n",
       "      <td>D</td>\n",
       "      <td>D4</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>40000.000000</td>\n",
       "      <td>2017/6/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>3</td>\n",
       "      <td>5118.676867</td>\n",
       "      <td>1118.68</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>8000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B4</td>\n",
       "      <td>1 year</td>\n",
       "      <td>RENT</td>\n",
       "      <td>42000.000000</td>\n",
       "      <td>2017/6/11</td>\n",
       "      <td>Charged Off</td>\n",
       "      <td>10</td>\n",
       "      <td>3962.340000</td>\n",
       "      <td>968.75</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>27000.000000</td>\n",
       "      <td>D</td>\n",
       "      <td>D5</td>\n",
       "      <td>6 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>83004.000000</td>\n",
       "      <td>2017/6/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>7</td>\n",
       "      <td>29820.276860</td>\n",
       "      <td>2820.28</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>25000.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C4</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>79000.000000</td>\n",
       "      <td>2017/5/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>13</td>\n",
       "      <td>33063.912780</td>\n",
       "      <td>8063.91</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>17600.000000</td>\n",
       "      <td>D</td>\n",
       "      <td>D4</td>\n",
       "      <td>8 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>72000.000000</td>\n",
       "      <td>2017/5/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>14</td>\n",
       "      <td>20172.786860</td>\n",
       "      <td>2572.79</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>20000.000000</td>\n",
       "      <td>D</td>\n",
       "      <td>D2</td>\n",
       "      <td>4 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>40000.000000</td>\n",
       "      <td>2017/5/11</td>\n",
       "      <td>Charged Off</td>\n",
       "      <td>6</td>\n",
       "      <td>23742.300000</td>\n",
       "      <td>8046.39</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>12400.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C2</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>65000.000000</td>\n",
       "      <td>2017/5/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>10</td>\n",
       "      <td>15146.533740</td>\n",
       "      <td>2746.53</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>6000.000000</td>\n",
       "      <td>D</td>\n",
       "      <td>D3</td>\n",
       "      <td>&lt; 1 year</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>60000.000000</td>\n",
       "      <td>2017/5/11</td>\n",
       "      <td>Charged Off</td>\n",
       "      <td>7</td>\n",
       "      <td>6543.590000</td>\n",
       "      <td>1584.75</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>12800.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A3</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>100000.000000</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>10</td>\n",
       "      <td>14211.312910</td>\n",
       "      <td>1411.31</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>2100.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>1 year</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>72000.000000</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>15</td>\n",
       "      <td>2357.178487</td>\n",
       "      <td>257.18</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>17325.000000</td>\n",
       "      <td>F</td>\n",
       "      <td>F2</td>\n",
       "      <td>9 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>35004.000000</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Charged Off</td>\n",
       "      <td>10</td>\n",
       "      <td>7437.120000</td>\n",
       "      <td>3758.96</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>5000.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C5</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>69000.000000</td>\n",
       "      <td>2017/3/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>7</td>\n",
       "      <td>6183.766196</td>\n",
       "      <td>1168.77</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>24175.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B5</td>\n",
       "      <td>9 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>95000.000000</td>\n",
       "      <td>2017/3/11</td>\n",
       "      <td>charged Off</td>\n",
       "      <td>8</td>\n",
       "      <td>24422.870000</td>\n",
       "      <td>4242.70</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>30000.000000</td>\n",
       "      <td>E</td>\n",
       "      <td>E1</td>\n",
       "      <td>1 year</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>100000.000000</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>14</td>\n",
       "      <td>44124.859950</td>\n",
       "      <td>14124.86</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>35000.000000</td>\n",
       "      <td>E</td>\n",
       "      <td>E1</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>106000.000000</td>\n",
       "      <td>2017/3/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>48845.292210</td>\n",
       "      <td>13845.29</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>18000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>82000.000000</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>20204.461240</td>\n",
       "      <td>2204.46</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>30000.000000</td>\n",
       "      <td>E</td>\n",
       "      <td>E1</td>\n",
       "      <td>1 year</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>100000.000000</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>14</td>\n",
       "      <td>44124.859950</td>\n",
       "      <td>14124.86</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>35000.000000</td>\n",
       "      <td>E</td>\n",
       "      <td>E1</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>106000.000000</td>\n",
       "      <td>2017/3/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>48845.292210</td>\n",
       "      <td>13845.29</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>18000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>82000.000000</td>\n",
       "      <td>2017/4/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>20204.461240</td>\n",
       "      <td>2204.46</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>10000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A4</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>58000.000000</td>\n",
       "      <td>2017/1/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>12</td>\n",
       "      <td>10994.614810</td>\n",
       "      <td>994.61</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>15000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>55000.000000</td>\n",
       "      <td>2017/10/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>14</td>\n",
       "      <td>16274.762910</td>\n",
       "      <td>1274.76</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>8000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B2</td>\n",
       "      <td>5 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>550000.000000</td>\n",
       "      <td>2017/9/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>5</td>\n",
       "      <td>9394.916746</td>\n",
       "      <td>1394.92</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>7200.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C5</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>68400.000000</td>\n",
       "      <td>2017/9/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>9</td>\n",
       "      <td>8844.469278</td>\n",
       "      <td>1644.47</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>14000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A3</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>OWN</td>\n",
       "      <td>71000.000000</td>\n",
       "      <td>2017/10/10</td>\n",
       "      <td>fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>15595.389910</td>\n",
       "      <td>1595.39</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>7500.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B5</td>\n",
       "      <td>1 year</td>\n",
       "      <td>RENT</td>\n",
       "      <td>35004.000000</td>\n",
       "      <td>2017/9/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>6</td>\n",
       "      <td>9978.050264</td>\n",
       "      <td>2478.05</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>6600.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>55800.000000</td>\n",
       "      <td>2017/7/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>8</td>\n",
       "      <td>7349.816860</td>\n",
       "      <td>749.82</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>12000.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C1</td>\n",
       "      <td>5 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>45600.000000</td>\n",
       "      <td>2017/7/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>7</td>\n",
       "      <td>13565.703560</td>\n",
       "      <td>1565.70</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>3686.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>62400.000000</td>\n",
       "      <td>2017/8/10</td>\n",
       "      <td>Charged Off</td>\n",
       "      <td>8</td>\n",
       "      <td>5207.370000</td>\n",
       "      <td>1520.88</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>2000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A1</td>\n",
       "      <td>n/a</td>\n",
       "      <td>OWN</td>\n",
       "      <td>16000.000000</td>\n",
       "      <td>2017/7/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>9</td>\n",
       "      <td>2203.348430</td>\n",
       "      <td>203.35</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>13000.000000</td>\n",
       "      <td>D</td>\n",
       "      <td>D4</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>25000.000000</td>\n",
       "      <td>2017/7/10</td>\n",
       "      <td>fully Paid</td>\n",
       "      <td>19</td>\n",
       "      <td>18442.068880</td>\n",
       "      <td>5442.07</td>\n",
       "      <td>D</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>1800.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C3</td>\n",
       "      <td>6 years</td>\n",
       "      <td>OWN</td>\n",
       "      <td>45600.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>11</td>\n",
       "      <td>2025.550714</td>\n",
       "      <td>225.55</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>6000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>82000.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>17</td>\n",
       "      <td>6745.258045</td>\n",
       "      <td>745.26</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50</th>\n",
       "      <td>7000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B3</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>102660.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>9</td>\n",
       "      <td>9052.310001</td>\n",
       "      <td>2052.31</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>51</th>\n",
       "      <td>25000.000000</td>\n",
       "      <td>E</td>\n",
       "      <td>E1</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>56000.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>6</td>\n",
       "      <td>19901.630490</td>\n",
       "      <td>3001.63</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52</th>\n",
       "      <td>4000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B2</td>\n",
       "      <td>2 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>16800.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>3</td>\n",
       "      <td>4318.218537</td>\n",
       "      <td>318.22</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>53</th>\n",
       "      <td>1800.000000</td>\n",
       "      <td>C</td>\n",
       "      <td>C3</td>\n",
       "      <td>6 years</td>\n",
       "      <td>OWN</td>\n",
       "      <td>45600.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>11</td>\n",
       "      <td>2025.550714</td>\n",
       "      <td>225.55</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>54</th>\n",
       "      <td>6000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A5</td>\n",
       "      <td>8 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>82000.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>17</td>\n",
       "      <td>6745.258045</td>\n",
       "      <td>745.26</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>55</th>\n",
       "      <td>7000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B3</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>102660.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>fully Paid</td>\n",
       "      <td>9</td>\n",
       "      <td>9052.310001</td>\n",
       "      <td>2052.31</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>56</th>\n",
       "      <td>19528.838235</td>\n",
       "      <td>E</td>\n",
       "      <td>E1</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>56000.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>6</td>\n",
       "      <td>19901.630490</td>\n",
       "      <td>3001.63</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>57</th>\n",
       "      <td>4000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B2</td>\n",
       "      <td>2 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>16800.000000</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>3</td>\n",
       "      <td>4318.218537</td>\n",
       "      <td>318.22</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58</th>\n",
       "      <td>6000.000000</td>\n",
       "      <td>D</td>\n",
       "      <td>D2</td>\n",
       "      <td>2 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>48000.000000</td>\n",
       "      <td>2017/4/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>7</td>\n",
       "      <td>7463.352797</td>\n",
       "      <td>1463.35</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59</th>\n",
       "      <td>25000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B4</td>\n",
       "      <td>6 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>60000.000000</td>\n",
       "      <td>2017/4/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>11</td>\n",
       "      <td>29462.617620</td>\n",
       "      <td>4462.62</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60</th>\n",
       "      <td>4000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B2</td>\n",
       "      <td>2 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>73199.676923</td>\n",
       "      <td>2017/5/10</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>3</td>\n",
       "      <td>4318.218537</td>\n",
       "      <td>318.22</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>24250.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B5</td>\n",
       "      <td>10+ years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>140000.000000</td>\n",
       "      <td>2017/12/9</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>15</td>\n",
       "      <td>27565.431850</td>\n",
       "      <td>3315.44</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>62</th>\n",
       "      <td>8000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B5</td>\n",
       "      <td>2 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>63000.000000</td>\n",
       "      <td>2017/12/9</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>7</td>\n",
       "      <td>9171.043721</td>\n",
       "      <td>1171.04</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>9000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B1</td>\n",
       "      <td>5 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>54000.000000</td>\n",
       "      <td>2017/11/9</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>12</td>\n",
       "      <td>10643.956490</td>\n",
       "      <td>1628.96</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>64</th>\n",
       "      <td>8000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A4</td>\n",
       "      <td>5 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>38000.000000</td>\n",
       "      <td>2017/11/9</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>6</td>\n",
       "      <td>8322.639678</td>\n",
       "      <td>322.64</td>\n",
       "      <td>A</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>65</th>\n",
       "      <td>2000.000000</td>\n",
       "      <td>A</td>\n",
       "      <td>A2</td>\n",
       "      <td>2 years</td>\n",
       "      <td>MORTGAGE</td>\n",
       "      <td>44000.000000</td>\n",
       "      <td>2017/11/9</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>11</td>\n",
       "      <td>2207.212632</td>\n",
       "      <td>207.21</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>66</th>\n",
       "      <td>8000.000000</td>\n",
       "      <td>B</td>\n",
       "      <td>B5</td>\n",
       "      <td>2 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>63000.000000</td>\n",
       "      <td>2017/12/9</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>7</td>\n",
       "      <td>9171.043721</td>\n",
       "      <td>1171.04</td>\n",
       "      <td>B</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>67</th>\n",
       "      <td>17600.000000</td>\n",
       "      <td>D</td>\n",
       "      <td>D4</td>\n",
       "      <td>8 years</td>\n",
       "      <td>RENT</td>\n",
       "      <td>72000.000000</td>\n",
       "      <td>2017/5/11</td>\n",
       "      <td>Fully Paid</td>\n",
       "      <td>14</td>\n",
       "      <td>20172.786860</td>\n",
       "      <td>2572.79</td>\n",
       "      <td>C</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>68 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       loan_amnt grade sub_grade emp_length home_ownership     annual_inc  \\\n",
       "0    5000.000000     B        B2  10+ years           RENT   24000.000000   \n",
       "1    2500.000000     C        C4   < 1 year           RENT   30000.000000   \n",
       "2   12500.000000     D        D4  10+ years           RENT   74400.000000   \n",
       "3   14000.000000     A        A4  10+ years           RENT   72000.000000   \n",
       "4   35000.000000     F        F5    4 years       MORTGAGE  100000.000000   \n",
       "5   16000.000000     C        C2  10+ years            OWN   73199.676923   \n",
       "6   14125.000000     B        B5  10+ years            OWN   60000.000000   \n",
       "7    5000.000000     B        B2    4 years           RENT   49000.000000   \n",
       "8    6000.000000     A        A5  10+ years       MORTGAGE   48000.000000   \n",
       "9   12000.000000     C        C3   < 1 year       MORTGAGE   81996.000000   \n",
       "10   4000.000000     D        D4  10+ years       MORTGAGE   40000.000000   \n",
       "11   8000.000000     B        B4     1 year           RENT   42000.000000   \n",
       "12  27000.000000     D        D5    6 years       MORTGAGE   83004.000000   \n",
       "13  25000.000000     C        C4  10+ years           RENT   79000.000000   \n",
       "14  17600.000000     D        D4    8 years           RENT   72000.000000   \n",
       "15  20000.000000     D        D2    4 years           RENT   40000.000000   \n",
       "16  12400.000000     C        C2  10+ years           RENT   65000.000000   \n",
       "17   6000.000000     D        D3   < 1 year       MORTGAGE   60000.000000   \n",
       "18  12800.000000     A        A3  10+ years       MORTGAGE  100000.000000   \n",
       "19   2100.000000     A        A5     1 year       MORTGAGE   72000.000000   \n",
       "20  17325.000000     F        F2    9 years           RENT   35004.000000   \n",
       "21   5000.000000     C        C5  10+ years       MORTGAGE   69000.000000   \n",
       "22  24175.000000     B        B5    9 years       MORTGAGE   95000.000000   \n",
       "23  30000.000000     E        E1     1 year       MORTGAGE  100000.000000   \n",
       "24  35000.000000     E        E1  10+ years           RENT  106000.000000   \n",
       "25  18000.000000     A        A5    8 years       MORTGAGE   82000.000000   \n",
       "26  30000.000000     E        E1     1 year       MORTGAGE  100000.000000   \n",
       "27  35000.000000     E        E1  10+ years           RENT  106000.000000   \n",
       "28  18000.000000     A        A5    8 years       MORTGAGE   82000.000000   \n",
       "29  10000.000000     A        A4  10+ years           RENT   58000.000000   \n",
       "..           ...   ...       ...        ...            ...            ...   \n",
       "38  15000.000000     A        A5  10+ years       MORTGAGE   55000.000000   \n",
       "39   8000.000000     B        B2    5 years       MORTGAGE  550000.000000   \n",
       "40   7200.000000     C        C5  10+ years           RENT   68400.000000   \n",
       "41  14000.000000     A        A3  10+ years            OWN   71000.000000   \n",
       "42   7500.000000     B        B5     1 year           RENT   35004.000000   \n",
       "43   6600.000000     A        A5    8 years       MORTGAGE   55800.000000   \n",
       "44  12000.000000     C        C1    5 years           RENT   45600.000000   \n",
       "45   3686.000000     B        B5    8 years       MORTGAGE   62400.000000   \n",
       "46   2000.000000     A        A1        n/a            OWN   16000.000000   \n",
       "47  13000.000000     D        D4  10+ years       MORTGAGE   25000.000000   \n",
       "48   1800.000000     C        C3    6 years            OWN   45600.000000   \n",
       "49   6000.000000     A        A5    8 years           RENT   82000.000000   \n",
       "50   7000.000000     B        B3  10+ years       MORTGAGE  102660.000000   \n",
       "51  25000.000000     E        E1  10+ years           RENT   56000.000000   \n",
       "52   4000.000000     B        B2    2 years           RENT   16800.000000   \n",
       "53   1800.000000     C        C3    6 years            OWN   45600.000000   \n",
       "54   6000.000000     A        A5    8 years           RENT   82000.000000   \n",
       "55   7000.000000     B        B3  10+ years       MORTGAGE  102660.000000   \n",
       "56  19528.838235     E        E1  10+ years           RENT   56000.000000   \n",
       "57   4000.000000     B        B2    2 years           RENT   16800.000000   \n",
       "58   6000.000000     D        D2    2 years           RENT   48000.000000   \n",
       "59  25000.000000     B        B4    6 years           RENT   60000.000000   \n",
       "60   4000.000000     B        B2    2 years           RENT   73199.676923   \n",
       "61  24250.000000     B        B5  10+ years       MORTGAGE  140000.000000   \n",
       "62   8000.000000     B        B5    2 years           RENT   63000.000000   \n",
       "63   9000.000000     B        B1    5 years       MORTGAGE   54000.000000   \n",
       "64   8000.000000     A        A4    5 years       MORTGAGE   38000.000000   \n",
       "65   2000.000000     A        A2    2 years       MORTGAGE   44000.000000   \n",
       "66   8000.000000     B        B5    2 years           RENT   63000.000000   \n",
       "67  17600.000000     D        D4    8 years           RENT   72000.000000   \n",
       "\n",
       "       issue_d    loan_status  open_acc   total_pymnt  total_rec_int  \\\n",
       "0   2017/12/11     Fully Paid         3   5863.155187         863.16   \n",
       "1   2017/12/11    Charged Off         3   1014.530000         435.17   \n",
       "2   2017/11/11     Fully Paid         8  14722.411910        2222.41   \n",
       "3   2017/11/11     fully Paid         6  15099.982530        1099.98   \n",
       "4   2017/10/11     Fully Paid         9  48743.119270       13743.12   \n",
       "5   2017/10/11    Charged Off        13   9202.000000        3800.05   \n",
       "6   2017/10/11     Fully Paid        13  16750.738520        2625.74   \n",
       "7    2017/9/11     Fully Paid        10   5863.155187         863.16   \n",
       "8    2017/9/11     Fully Paid        11   7235.598134        1235.60   \n",
       "9   2017/10/11     Fully Paid        11  16996.739990        4996.74   \n",
       "10   2017/6/11     Fully Paid         3   5118.676867        1118.68   \n",
       "11   2017/6/11    Charged Off        10   3962.340000         968.75   \n",
       "12   2017/6/11     Fully Paid         7  29820.276860        2820.28   \n",
       "13   2017/5/11     Fully Paid        13  33063.912780        8063.91   \n",
       "14   2017/5/11     Fully Paid        14  20172.786860        2572.79   \n",
       "15   2017/5/11    Charged Off         6  23742.300000        8046.39   \n",
       "16   2017/5/11     Fully Paid        10  15146.533740        2746.53   \n",
       "17   2017/5/11    Charged Off         7   6543.590000        1584.75   \n",
       "18   2017/4/11     Fully Paid        10  14211.312910        1411.31   \n",
       "19   2017/4/11     Fully Paid        15   2357.178487         257.18   \n",
       "20   2017/4/11    Charged Off        10   7437.120000        3758.96   \n",
       "21   2017/3/11     Fully Paid         7   6183.766196        1168.77   \n",
       "22   2017/3/11    charged Off         8  24422.870000        4242.70   \n",
       "23   2017/4/11     Fully Paid        14  44124.859950       14124.86   \n",
       "24   2017/3/11     Fully Paid         8  48845.292210       13845.29   \n",
       "25   2017/4/11     Fully Paid         8  20204.461240        2204.46   \n",
       "26   2017/4/11     Fully Paid        14  44124.859950       14124.86   \n",
       "27   2017/3/11     Fully Paid         8  48845.292210       13845.29   \n",
       "28   2017/4/11     Fully Paid         8  20204.461240        2204.46   \n",
       "29   2017/1/11     Fully Paid        12  10994.614810         994.61   \n",
       "..         ...            ...       ...           ...            ...   \n",
       "38  2017/10/10     Fully Paid        14  16274.762910        1274.76   \n",
       "39   2017/9/10     Fully Paid         5   9394.916746        1394.92   \n",
       "40   2017/9/10     Fully Paid         9   8844.469278        1644.47   \n",
       "41  2017/10/10     fully Paid         8  15595.389910        1595.39   \n",
       "42   2017/9/10     Fully Paid         6   9978.050264        2478.05   \n",
       "43   2017/7/10     Fully Paid         8   7349.816860         749.82   \n",
       "44   2017/7/10     Fully Paid         7  13565.703560        1565.70   \n",
       "45   2017/8/10    Charged Off         8   5207.370000        1520.88   \n",
       "46   2017/7/10     Fully Paid         9   2203.348430         203.35   \n",
       "47   2017/7/10     fully Paid        19  18442.068880        5442.07   \n",
       "48   2017/5/10     Fully Paid        11   2025.550714         225.55   \n",
       "49   2017/5/10     Fully Paid        17   6745.258045         745.26   \n",
       "50   2017/5/10     Fully Paid         9   9052.310001        2052.31   \n",
       "51   2017/5/10     Fully Paid         6  19901.630490        3001.63   \n",
       "52   2017/5/10     Fully Paid         3   4318.218537         318.22   \n",
       "53   2017/5/10     Fully Paid        11   2025.550714         225.55   \n",
       "54   2017/5/10     Fully Paid        17   6745.258045         745.26   \n",
       "55   2017/5/10     fully Paid         9   9052.310001        2052.31   \n",
       "56   2017/5/10     Fully Paid         6  19901.630490        3001.63   \n",
       "57   2017/5/10     Fully Paid         3   4318.218537         318.22   \n",
       "58   2017/4/10     Fully Paid         7   7463.352797        1463.35   \n",
       "59   2017/4/10     Fully Paid        11  29462.617620        4462.62   \n",
       "60   2017/5/10     Fully Paid         3   4318.218537         318.22   \n",
       "61   2017/12/9     Fully Paid        15  27565.431850        3315.44   \n",
       "62   2017/12/9     Fully Paid         7   9171.043721        1171.04   \n",
       "63   2017/11/9     Fully Paid        12  10643.956490        1628.96   \n",
       "64   2017/11/9     Fully Paid         6   8322.639678         322.64   \n",
       "65   2017/11/9     Fully Paid        11   2207.212632         207.21   \n",
       "66   2017/12/9     Fully Paid         7   9171.043721        1171.04   \n",
       "67   2017/5/11     Fully Paid        14  20172.786860        2572.79   \n",
       "\n",
       "   categories  \n",
       "0           A  \n",
       "1           A  \n",
       "2           B  \n",
       "3           A  \n",
       "4           B  \n",
       "5           C  \n",
       "6           C  \n",
       "7           B  \n",
       "8           B  \n",
       "9           B  \n",
       "10          A  \n",
       "11          B  \n",
       "12          B  \n",
       "13          C  \n",
       "14          C  \n",
       "15          A  \n",
       "16          B  \n",
       "17          B  \n",
       "18          B  \n",
       "19          C  \n",
       "20          B  \n",
       "21          B  \n",
       "22          B  \n",
       "23          C  \n",
       "24          B  \n",
       "25          B  \n",
       "26          C  \n",
       "27          B  \n",
       "28          B  \n",
       "29          B  \n",
       "..        ...  \n",
       "38          C  \n",
       "39          A  \n",
       "40          B  \n",
       "41          B  \n",
       "42          A  \n",
       "43          B  \n",
       "44          B  \n",
       "45          B  \n",
       "46          B  \n",
       "47          D  \n",
       "48          B  \n",
       "49          C  \n",
       "50          B  \n",
       "51          A  \n",
       "52          A  \n",
       "53          B  \n",
       "54          C  \n",
       "55          B  \n",
       "56          A  \n",
       "57          A  \n",
       "58          B  \n",
       "59          B  \n",
       "60          A  \n",
       "61          C  \n",
       "62          B  \n",
       "63          B  \n",
       "64          A  \n",
       "65          B  \n",
       "66          B  \n",
       "67          C  \n",
       "\n",
       "[68 rows x 12 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_loan_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "根据属性`home_ownership`,对用户的年收入(属性`annual_inc`)，进行分类统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "home_ownership\nMORTGAGE    92309.333687\nOWN         52542.810989\nRENT        60400.239904\nName: annual_inc, dtype: float64"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 各分组年收入中位数\n",
    "group_value = loan_data['annual_inc'].groupby(loan_data['home_ownership'])\n",
    "group_value.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 各分组贷款数额中位数\n",
    "group_value1 = loan_data['loan_amnt'].groupby(loan_data['home_ownership']) \n",
    "group_value1.max()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10 One-Hot编码 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    loan_amnt grade sub_grade emp_length home_ownership  annual_inc  \\\n0      5000.0     B        B2  10+ years           RENT     24000.0   \n1      2500.0     C        C4   < 1 year           RENT     30000.0   \n2     12500.0     D        D4  10+ years           RENT     74400.0   \n3     14000.0     A        A4  10+ years           RENT     72000.0   \n4     35000.0     F        F5    4 years       MORTGAGE    100000.0   \n5     16000.0     C        C2  10+ years            OWN         NaN   \n6     14125.0     B        B5  10+ years            OWN     60000.0   \n7      5000.0     B        B2    4 years           RENT     49000.0   \n8      6000.0     A        A5  10+ years       MORTGAGE     48000.0   \n9     12000.0     C        C3   < 1 year       MORTGAGE     81996.0   \n10     4000.0     D        D4  10+ years       MORTGAGE     40000.0   \n11     8000.0     B        B4     1 year           RENT     42000.0   \n12    27000.0     D        D5    6 years       MORTGAGE     83004.0   \n13    25000.0     C        C4  10+ years           RENT     79000.0   \n14    17600.0     D        D4    8 years           RENT     72000.0   \n15    20000.0     D        D2    4 years           RENT     40000.0   \n16    12400.0     C        C2  10+ years           RENT     65000.0   \n17     6000.0     D        D3   < 1 year       MORTGAGE     60000.0   \n18    12800.0     A        A3  10+ years       MORTGAGE    100000.0   \n19     2100.0     A        A5     1 year       MORTGAGE     72000.0   \n20    17325.0     F        F2    9 years           RENT     35004.0   \n21     5000.0     C        C5  10+ years       MORTGAGE     69000.0   \n22    24175.0     B        B5    9 years       MORTGAGE     95000.0   \n23    30000.0     E        E1     1 year       MORTGAGE    100000.0   \n24    35000.0     E        E1  10+ years           RENT    106000.0   \n25    18000.0     A        A5    8 years       MORTGAGE     82000.0   \n26    30000.0     E        E1     1 year       MORTGAGE    100000.0   \n27    35000.0     E        E1  10+ years           RENT    106000.0   \n28    18000.0     A        A5    8 years       MORTGAGE     82000.0   \n29        NaN     A        A4  10+ years           RENT     58000.0   \n..        ...   ...       ...        ...            ...         ...   \n38    15000.0     A        A5  10+ years       MORTGAGE     55000.0   \n39     8000.0     B        B2    5 years       MORTGAGE    550000.0   \n40     7200.0     C        C5  10+ years           RENT     68400.0   \n41    14000.0     A        A3  10+ years            OWN     71000.0   \n42     7500.0     B        B5     1 year           RENT     35004.0   \n43     6600.0     A        A5    8 years       MORTGAGE     55800.0   \n44    12000.0     C        C1    5 years           RENT     45600.0   \n45        NaN     B        B5    8 years       MORTGAGE     62400.0   \n46     2000.0     A        A1        NaN            OWN     16000.0   \n47    13000.0     D        D4  10+ years       MORTGAGE     25000.0   \n48     1800.0     C        C3    6 years            OWN     45600.0   \n49     6000.0     A        A5    8 years           RENT     82000.0   \n50     7000.0     B        B3  10+ years       MORTGAGE    102660.0   \n51    25000.0     E        E1  10+ years           RENT     56000.0   \n52     4000.0     B        B2    2 years           RENT     16800.0   \n53     1800.0     C        C3    6 years            OWN     45600.0   \n54     6000.0     A        A5    8 years           RENT     82000.0   \n55     7000.0     B        B3  10+ years       MORTGAGE    102660.0   \n56   500000.0     E        E1  10+ years           RENT     56000.0   \n57     4000.0     B        B2    2 years           RENT     16800.0   \n58     6000.0     D        D2    2 years           RENT     48000.0   \n59    25000.0     B        B4    6 years           RENT     60000.0   \n60     4000.0     B        B2    2 years           RENT         NaN   \n61    24250.0     B        B5  10+ years       MORTGAGE    140000.0   \n62     8000.0     B        B5    2 years           RENT     63000.0   \n63     9000.0     B        B1    5 years       MORTGAGE     54000.0   \n64     8000.0     A        A4    5 years       MORTGAGE     38000.0   \n65     2000.0     A        A2    2 years       MORTGAGE     44000.0   \n66     8000.0     B        B5    2 years           RENT     63000.0   \n67    17600.0     D        D4    8 years           RENT     72000.0   \n\n       issue_d    loan_status  open_acc   total_pymnt  total_rec_int  \n0   2017/12/11     Fully Paid         3   5863.155187         863.16  \n1   2017/12/11    Charged Off         3   1014.530000         435.17  \n2   2017/11/11     Fully Paid         8  14722.411910        2222.41  \n3   2017/11/11     fully Paid         6  15099.982530        1099.98  \n4   2017/10/11     Fully Paid         9  48743.119270       13743.12  \n5   2017/10/11    Charged Off        13   9202.000000        3800.05  \n6   2017/10/11     Fully Paid        13  16750.738520        2625.74  \n7    2017/9/11     Fully Paid        10   5863.155187         863.16  \n8    2017/9/11     Fully Paid        11   7235.598134        1235.60  \n9   2017/10/11     Fully Paid        11  16996.739990        4996.74  \n10   2017/6/11     Fully Paid         3   5118.676867        1118.68  \n11   2017/6/11    Charged Off        10   3962.340000         968.75  \n12   2017/6/11     Fully Paid         7  29820.276860        2820.28  \n13   2017/5/11     Fully Paid        13  33063.912780        8063.91  \n14   2017/5/11     Fully Paid        14  20172.786860        2572.79  \n15   2017/5/11    Charged Off         6  23742.300000        8046.39  \n16   2017/5/11     Fully Paid        10  15146.533740        2746.53  \n17   2017/5/11    Charged Off         7   6543.590000        1584.75  \n18   2017/4/11     Fully Paid        10  14211.312910        1411.31  \n19   2017/4/11     Fully Paid        15   2357.178487         257.18  \n20   2017/4/11    Charged Off        10   7437.120000        3758.96  \n21   2017/3/11     Fully Paid         7   6183.766196        1168.77  \n22   2017/3/11    charged Off         8  24422.870000        4242.70  \n23   2017/4/11     Fully Paid        14  44124.859950       14124.86  \n24   2017/3/11     Fully Paid         8  48845.292210       13845.29  \n25   2017/4/11     Fully Paid         8  20204.461240        2204.46  \n26   2017/4/11     Fully Paid        14  44124.859950       14124.86  \n27   2017/3/11     Fully Paid         8  48845.292210       13845.29  \n28   2017/4/11     Fully Paid         8  20204.461240        2204.46  \n29   2017/1/11     Fully Paid        12  10994.614810         994.61  \n..         ...            ...       ...           ...            ...  \n38  2017/10/10     Fully Paid        14  16274.762910        1274.76  \n39   2017/9/10     Fully Paid         5   9394.916746        1394.92  \n40   2017/9/10     Fully Paid         9   8844.469278        1644.47  \n41  2017/10/10     fully Paid         8  15595.389910        1595.39  \n42   2017/9/10     Fully Paid         6   9978.050264        2478.05  \n43   2017/7/10     Fully Paid         8   7349.816860         749.82  \n44   2017/7/10     Fully Paid         7  13565.703560        1565.70  \n45   2017/8/10    Charged Off         8   5207.370000        1520.88  \n46   2017/7/10     Fully Paid         9   2203.348430         203.35  \n47   2017/7/10     fully Paid        19  18442.068880        5442.07  \n48   2017/5/10     Fully Paid        11   2025.550714         225.55  \n49   2017/5/10     Fully Paid        17   6745.258045         745.26  \n50   2017/5/10     Fully Paid         9   9052.310001        2052.31  \n51   2017/5/10     Fully Paid         6  19901.630490        3001.63  \n52   2017/5/10     Fully Paid         3   4318.218537         318.22  \n53   2017/5/10     Fully Paid        11   2025.550714         225.55  \n54   2017/5/10     Fully Paid        17   6745.258045         745.26  \n55   2017/5/10     fully Paid         9   9052.310001        2052.31  \n56   2017/5/10     Fully Paid         6  19901.630490        3001.63  \n57   2017/5/10     Fully Paid         3   4318.218537         318.22  \n58   2017/4/10     Fully Paid         7   7463.352797        1463.35  \n59   2017/4/10     Fully Paid        11  29462.617620        4462.62  \n60   2017/5/10     Fully Paid         3   4318.218537         318.22  \n61   2017/12/9     Fully Paid        15  27565.431850        3315.44  \n62   2017/12/9     Fully Paid         7   9171.043721        1171.04  \n63   2017/11/9     Fully Paid        12  10643.956490        1628.96  \n64   2017/11/9     Fully Paid         6   8322.639678         322.64  \n65   2017/11/9     Fully Paid        11   2207.212632         207.21  \n66   2017/12/9     Fully Paid         7   9171.043721        1171.04  \n67   2017/5/11     Fully Paid        14  20172.786860        2572.79  \n\n[68 rows x 11 columns]\n"
     ]
    }
   ],
   "source": [
    "dummies = pd.get_dummies(loan_data['grade'],prefix='grade')\n",
    "pd.concat([loan_data,dummies],axis=1) # 拼接回原来的数据结构\n",
    "print(loan_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><font color=\"#888\">Copyright &copy; 2017 Sid Ian@Boya, <a href=\"mailto:xiaodongy@boyabigdata.cn\">xiaodongy@boyabigdata.cn</a>. All Rights Reserved.</font></center>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
